package com.eshipenet.shipnet
import org.joda.time.DateTime
import play.api.libs.json._

package object edgeboxextbuilder {
  implicit val jodaDateTimeReads: Reads[DateTime] = Reads[DateTime] { js =>
    js.validate[String].map[DateTime](dtString => DateTime.parse(dtString))
    js match {
      case JsString(str) => JsSuccess(DateTime.parse(str))
      case JsNumber(num) => JsSuccess(new DateTime(num.toLong))
      case other         => JsError(s"invalid DateTime format: $other")
    }
  }

  implicit val jodaDateTimeWrites: Writes[DateTime] = new Writes[DateTime] {
    override def writes(o: DateTime): JsValue = JsString(o.toString())
  }
}
